<!DOCTYPE html>
<html lang="en" xmlns:sec="http://www.thymeleaf.org/extras/spring-security" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="common :: header('角色管理')"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <table id="slide-table" lay-filter="slide-table"></table>
    </div>
</div>
</body>

<script id="config-toolbar" type="text/html">
    <button sec:authorize="hasPermission('/admin/slide/add','')"
            class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
</script>

<script id="user-bar" type="text/html">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="rbac"><i class="layui-icon layui-icon-vercode"></i>
    </button>
    <button sec:authorize="hasPermission('/admin/role/edit','sys:dept:edit')"
            class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <button sec:authorize="hasPermission('/admin/role/delete','sys:dept:delete')"
            class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>
</script>

<th:block th:include="common :: footer"/>
<script>
    layui.use(['table', 'form', 'jquery', 'common', 'popup'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let popup = layui.popup;

        let MODULE_PATH = "/admin/role/";

        let cols = [
            [
                //{type: 'checkbox'},
                {title: 'ID', field: 'id', align: 'center', width: 120},
                {title: '名称', field: 'name', align: 'center'},
                {title: '备注', field: 'remark', align: 'center'},
                {title: '状态', field: 'status', align: 'center'},
                {title: '操作',fixed:"right", toolbar: '#user-bar', align: 'center', width: 330}
            ]
        ]

        table.render({
            elem: '#slide-table',
            url: MODULE_PATH + 'list',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#config-toolbar',
            parseData: function (res) {
              return {
                    "code": 0, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": res.data //解析数据列表
                };
            },
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }]
        });

        table.on('tool(slide-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }else if(obj.event === 'rbac'){
                window.rbac(obj);
            }
        });

        table.on('toolbar(slide-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(config-query)', function (data) {
            window.refresh(data.field);
            return false;
        });

        window.add = function () {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['550px', '400px'],
                content: MODULE_PATH + 'add'
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['550px', '400px'],
                content: MODULE_PATH + 'edit?id=' + obj.data['id']
            });
        }
        window.rbac = function (obj) {
            layer.open({
                type: 2,
                title: '授权',
                shade: 0.1,
                area: ['550px', '400px'],
                content: MODULE_PATH + 'rbac?id=' + obj.data['id']
            });
        }
        window.remove = function (obj) {
            layer.confirm('确定要删除', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + "delete/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code) {
                            popup.success(result.msg, function () {
                                table.reload('slide-table');
                            });
                        } else {
                            popup.failure(result.msg);
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('slide-table', {where: param});
        }
    })
</script>
</html>